package les.projeto;

import java.util.ArrayList;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


public class GPACalc extends Activity{
	

	private String[][] aboutSubjects = null;
	private String[] bdTeachers = null;
	private String[] bdLocals = null;
	private String nota1 = "";
	private String nota2 = "";
	private String nota3 = "";
	private Double cre = 0.0;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.gpa_cre);
	    
	    BD bd = new BD();
	    ArrayList<String> profs = bd.getTeachers(this);
	    
		String[][] teachersAux = new String[profs.size()][3];
	    
		bdTeachers = new String[teachersAux.length];
	    for (int i = 0; i < profs.size(); i++) {
			String[] date = profs.get(i).split(" ");
			bdTeachers[i] = date[0];
			
		}
	    
//	    ArrayList<String> locais = bd.getLocal(this);
//		String[][] localsAux = new String[locais.size()][3];
//		bdLocals = new String[localsAux.length];
//	    for (int i = 0; i < profs.size(); i++) {
//			String[] date = locais.get(i).split(" ");
//			bdLocals[i] = date[0] + " " + date[1];
//		}
	    
	    updateListValues();
	    ListView subjects = (ListView)findViewById(R.id.listViewSubjects);
	    subjects.setOnItemClickListener(new OnItemClickListener() {
	    	@Override
	    	public void onItemClick(AdapterView<?> parent, View view,
	    		int position, long id) {
	    		showSubjects(aboutSubjects[position][0], aboutSubjects[position][1],
		    					aboutSubjects[position][2], aboutSubjects[position][3], aboutSubjects[position][4]);
	    	}
	    });
	    
	    Button addLocal = (Button)findViewById(R.id.buttonCRE);
        addLocal.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                viewCRE();
                
            }
        });
	    
	}
	
	protected void viewCRE() {
        final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
        helpBuilder.setTitle("CRE: " + cre);
        helpBuilder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
	    });
	    AlertDialog helpDialog = helpBuilder.create();
	    helpDialog.show();
    }

    private void updateListValues() {
	    ListView subjects = (ListView)findViewById(R.id.listViewSubjects);

	    
	    BD bd = new BD();
	    //ArrayList<String> subjectsList = bd.getSubjects(this);
	    ArrayList<String> subjectsList = new ArrayList<String>();
	    //subjectsList.add("DISCIPLINA CR PERIODO MEDIA SITUACAO");
	    subjectsList.add("FISICA 4 2011.1 9.0 APROVADO");
	    subjectsList.add("CalculoII 4 2011.1 8.0 APROVADO");
	    for (int i = 0; i < subjectsList.size(); i++) {
			System.out.println(subjectsList.get(i));
		}
		aboutSubjects = new String[subjectsList.size()][5];
		
	    for (int i = 0; i < subjectsList.size(); i++) {
			String[] date = subjectsList.get(i).split(" ");
			aboutSubjects[i][0] = date[0];
			aboutSubjects[i][1] = date[1];
			aboutSubjects[i][2] = date[2];
			aboutSubjects[i][3] = date[3];
			aboutSubjects[i][4] = date[4];
		}
	    Double soma = 0.0;
	    int creditos = 0;
	    for (int i = 0; i <  subjectsList.size(); i++) {
            soma = soma +  Double.parseDouble(aboutSubjects[i][3]) * Integer.parseInt(aboutSubjects[i][1]);
            creditos = creditos + Integer.parseInt(aboutSubjects[i][1]);
        }
	    
	    cre = soma / creditos;
	    
	    String[] values = new String[aboutSubjects.length];
	    for (int i = 0; i < values.length; i++) {
			values[i] = aboutSubjects[i][0] + "   " + aboutSubjects[i][1] + "   " + aboutSubjects[i][2] + "   " 
	    + aboutSubjects[i][3] + "   " + aboutSubjects[i][4];
		}
	    
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	    		android.R.layout.simple_list_item_1, android.R.id.text1, values);
	    subjects.setAdapter(adapter);
	    subjects.invalidate();
	}

	private void showSubjects(final String name, final String description, final String teacher, final String local, final String hour) {
		 final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle(name);
		 helpBuilder.setMessage("Description: " + description + "\nTeacher: " + teacher + "\nPlace: " + local + "\nHour: " + hour);
		 helpBuilder.setPositiveButton("Add notes", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  addNotes();
				  dialog.cancel();
			  }
		 });

		 helpBuilder.setNeutralButton("Edit", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  dialog.cancel();
			  }
			 });
		 
		 helpBuilder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  dialog.cancel();
			  }
		 });
		
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
		}
	
	private void addNotes() {
		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle("Add Notes");
		 helpBuilder.setMessage("Add note 1: ");
		 final EditText input = new EditText(this);
		 input.setSingleLine();
		 input.setText("");
		 helpBuilder.setView(input);
	
		 helpBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  nota1 = "";
				  nota1 = input.getText().toString();
				  dialog.cancel();
				  addNote2();
			  }
		 });
		 
		 helpBuilder.setNeutralButton("Save", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  dialog.cancel();
			  }
		 });

		 helpBuilder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
			 @Override
			 public void onClick(DialogInterface dialog, int which) {
				 dialog.cancel();
			 }
		 });
		 
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
	}
	
	protected void addNote2() {
		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle("Add notes");
		 helpBuilder.setMessage("Add note 2: ");
		 final EditText input = new EditText(this);
		 input.setSingleLine();
		 input.setText("");
		 helpBuilder.setView(input);
	
		 helpBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  nota2 = "";
				  nota2 = input.getText().toString();
				  dialog.cancel();
				  addNote3();
			  }
		 });
		 helpBuilder.setNeutralButton("Save", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  dialog.cancel();
			  }
		 });

		 helpBuilder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
			 @Override
			 public void onClick(DialogInterface dialog, int which) {
				 dialog.cancel();
			 }
		 });
		 
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
		
	}
	
	protected void addNote3() {
		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle("Add notes");
		 helpBuilder.setMessage("Add note 3: ");
		 final EditText input = new EditText(this);
		 input.setSingleLine();
		 input.setText("");
		 helpBuilder.setView(input);
	
		 helpBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  nota3 = "";
				  nota3 = input.getText().toString();
				  System.out.println(Integer.parseInt(nota1) + Integer.parseInt(nota2) + Integer.parseInt(nota3));
				  dialog.cancel();
				  
			  }
		 });
		 helpBuilder.setNeutralButton("Save", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  dialog.cancel();
			  }
		 });

		 helpBuilder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
			 @Override
			 public void onClick(DialogInterface dialog, int which) {
				 dialog.cancel();
			 }
		 });
		 
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
		
	}
	
	
	@Override
	public void onBackPressed() {
		
        back();	        		 
        return;
	}

	private void back() {
		Calendar date = Calendar.getInstance();
		int month = date.get(Calendar.MONTH);
        int year = date.get(Calendar.YEAR);
        int today = date.get(Calendar.DAY_OF_MONTH);
        
	    Intent trocatela = new Intent(GPACalc.this, CalendarView.class);
		trocatela.putExtra("date", year+"-"+month+"-"+today);

		GPACalc.this.startActivity(trocatela);
		GPACalc.this.finish();
	}
}
